<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="30%"
  >
    <el-form
      :model="memberLevel"
      ref="memberLevel"
      label-width="100px"
    >
      <el-form-item label="等级名称">
        <el-input
          v-model="memberLevel.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="需成长值">
        <el-input-number
          v-model="memberLevel.growthPoint"
          :step="200"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="免邮标准">
        <el-input-number v-model="memberLevel.freeFreightPoint"></el-input-number>
      </el-form-item>
      <el-form-item label="评价成长值">
        <el-input-number
          v-model="memberLevel.commentGrowthPoint"
          :step="20"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="默认等级">
        <el-checkbox
          v-model="memberLevel.defaultStatus"
          :true-label="1"
          :false-label="0"
        ></el-checkbox>
      </el-form-item>
      <el-form-item label="免邮特权">
        <el-checkbox
          v-model="memberLevel.priviledgeFreeFreight"
          :true-label="1"
          :false-label="0"
        ></el-checkbox>
      </el-form-item>
      <el-form-item label="特价特权">
        <el-checkbox
          v-model="memberLevel.priviledgeMemberPrice"
          :true-label="1"
          :false-label="0"
        ></el-checkbox>
      </el-form-item>
      <el-form-item label="生日特权">
        <el-checkbox
          v-model="memberLevel.priviledgeBirthday"
          :true-label="1"
          :false-label="0"
        ></el-checkbox>
      </el-form-item>
      <el-form-item label="备注">
        <el-input
          v-model="memberLevel.note"
          autocomplete="off"
        ></el-input>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit('memberLevel')"
      >确定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data () {
    return {
      title: '',
      visible: false,
      memberLevel: {
        id: '',
        name: '',
        growthPoint: 0,
        freeFreightPoint: 299,
        commentGrowthPoint: 0,
        defaultStatus: 0,
        priviledgeFreeFreight: 0,
        priviledgeMemberPrice: 0,
        priviledgeBirthday: 0,
        note: ''
      },
      dialogType: ''
    }
  },
  methods: {
    init (index, row) {
      this.visible = true
      if (Number.isNaN(parseInt(index))) {
        this.title = '添加会员等级'
        this.dialogType = 'add'
        this.addEchoMemberLevel()
      } else {
        this.title = '修改会员等级'
        this.dialogType = 'edit'
        this.row = row
        this.editEchoMemberLevel(this.row)
      }
    },
    addEchoMemberLevel () {
      this.memberLevel.name = ''
      this.memberLevel.growthPoint = 0
      this.memberLevel.freeFreightPoint = 299
      this.memberLevel.commentGrowthPoint = 0
      this.memberLevel.defaultStatus = 0
      this.memberLevel.priviledgeFreeFreight = 0
      this.memberLevel.priviledgeMemberPrice = 0
      this.memberLevel.priviledgeBirthday = 0
      this.memberLevel.note = ''
    },
    editEchoMemberLevel (row) {
      this.$http({
        url: this.$http.adornUrl(`/member/memberlevel/info/${row.id}`),
        method: 'get'
        // params: this.$http.adornParams({

        // })
      }).then(({
        data
      }) => {
        // 回显
        this.memberLevel.id = data.data.id
        this.memberLevel.name = data.data.name
        this.memberLevel.growthPoint = data.data.growthPoint
        this.memberLevel.freeFreightPoint = data.data.freeFreightPoint
        this.memberLevel.commentGrowthPoint = data.data.commentGrowthPoint
        this.memberLevel.defaultStatus = data.data.defaultStatus
        this.memberLevel.priviledgeFreeFreight = data.data.priviledgeFreeFreight
        this.memberLevel.priviledgeMemberPrice = data.data.priviledgeMemberPrice
        this.memberLevel.priviledgeBirthday = data.data.priviledgeBirthday
        this.memberLevel.note = data.data.note
      })
    },
    addMemberLevel () {
      this.$http({
        url: this.$http.adornUrl('/member/memberlevel/save'),
        method: 'post',
        data: this.$http.adornData(this.memberLevel, false)
      }).then(({
        data
      }) => {
        this.$message({
          type: 'success',
          message: '会员等级添加成功!'
        })
        this.visible = false // 关闭对话框
        this.$emit('refreshDataList') // 刷新数据
      })
    },
    editMemberLevel () {
      this.$http({
        url: this.$http.adornUrl('/member/memberlevel/update'),
        method: 'post',
        data: this.$http.adornData(this.memberLevel, false)
      }).then(({
        data
      }) => {
        this.$message({
          type: 'success',
          message: '会员等级修改成功!'
        })
        this.visible = false // 关闭对话框
        this.$emit('refreshDataList') // 刷新数据
      })
    },
    dataFormSubmit (memberLevel) {
      this.$refs[memberLevel].validate((valid) => {
        if (valid) {
          if (this.dialogType === 'add') {
            this.addMemberLevel()
          } else {
            this.editMemberLevel()
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style>
</style>